package ue.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class SessionUtils {
    
	private static String  CURRENTUSER = "CURRENT_USER";
	
	public static Integer getUserId(){
		
		Integer currentUserId = null;
		
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		HttpSession session = request.getSession();
		
		Object sessionObj =   session.getAttribute(CURRENTUSER);
		
		if(sessionObj!=null){
			
			currentUserId = Integer.valueOf(sessionObj.toString());
		}
		
		return currentUserId;
	}
	
	
	public static void putUser(Integer userId){
		
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		HttpSession session = request.getSession();
		
		
		session.setAttribute(CURRENTUSER, userId);
		
	}
	
}
